package com.atguigu.edu.realtime220815.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Author: rym
 * @Description: TODO
 * @DateTime: 2023/2/16 17:13
 **/
public class RedisUtil {
    //获取连接，通过连接池
    private static JedisPool jedisPool;
    //每次获取连接的时候，创建池创建一个就行
    //该代码为饿汉单例模式：在类加载的时候直接把当前对象创建出来，类只会加载一次，代码只会执行一次
    static {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(100);
        poolConfig.setMaxIdle(5);
        poolConfig.setMinIdle(5);
        poolConfig.setBlockWhenExhausted(true);
        poolConfig.setMaxWaitMillis(2000);
        poolConfig.setTestOnBorrow(true);

        jedisPool = new JedisPool(poolConfig,"hadoop102",6379,10000);
    }

    public static Jedis getJedis(){
        System.out.println("~~~获取Jedis客户端~~~");
        //通过连接池获取连接对象
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }
}
